package com.stone.business.workcard.service;

import java.util.List;

import com.stone.business.workcard.pojo.Gxzlkzk;
import com.stone.business.workcard.pojo.Gxzlkzk_BZ;
import com.stone.business.workcard.pojo.MainWorkCardOne;
import com.stone.business.workcard.pojo.Ryzzfg;
import com.stone.business.workcard.pojo.WorkCardOne_BZ;
import com.stone.business.workcard.pojo.Wxdfx;
import com.stone.common.pojo.TFile;

import frm.ssh.exception.AppException;
import frm.ssh.service.BaseService;

public class WorkcardService extends BaseService{

	/**
	 * 保存班组作业控制卡（一种票）
	 * @param log
	 */
	public void save(MainWorkCardOne mainWorkCardOne) {
		super.save(mainWorkCardOne);
	}
	/**
	 * 更新
	 * @param set 要改变的值
	 * @param ids 条件
	 * @param objectSimpleName
	 */
	public void update(final String set,final String ids,final String objectSimpleName){		
		super.updateByDHql(set, ids, null, null, objectSimpleName);
	}
	/**
	 * 删除多条记录
	 * 
	 * @param ids主键组合 以','号隔开
	 * @param objectSimpleName pojo
	 * @throws AppException
	 */
	public void doDelAll(final String ids,final String objectSimpleName) {
		super.deleteAll(ids, objectSimpleName);
	}
	/**
	 * 删除多条记录
	 * 
	 * @param ids主键组合 以','号隔开
	 * @param objectSimpleName pojo
	 * @throws AppException
	 */
	public void doDelAllBYDHSql(final String ids,final String objectSimpleName) {
		super.deleteAll(ids, objectSimpleName);
		super.deleteByDHql(" p_idsno in ("+ids+")", null, objectSimpleName);
	}	
	public String doSave(MainWorkCardOne entity, List<WorkCardOne_BZ> list,TFile file) {

		String strbz = "";
		
		if (entity.getId_sno() == 0) {
			if(file!=null){
			super.save(file);
			entity.setPic_id(file.getId());
			}
			super.save(entity);
			
			if(list!=null)
			for (WorkCardOne_BZ bz : list) {		
				if(bz!=null){
				bz.setP_idsno(entity.getId_sno());
				super.save(bz);
				}
			}			
			
		}else{
			if(file!=null){
				
				super.deleteAll(String.valueOf(entity.getPic_id()), TFile.class.getName());
				super.save(file);				
				entity.setPic_id(file.getId());
				}
				super.update(entity);
				
				if(list!=null){
					super.deleteByDHql(" p_idsno='"+entity.getId_sno()+"' ", null, WorkCardOne_BZ.class.getName());
					
				for (WorkCardOne_BZ bz : list) {	
					if(bz!=null){
					bz.setP_idsno(entity.getId_sno());
					bz.setQuetionandsuggest(bz.getQuetionandsuggest().trim());
					super.save(bz);
					}
				}		
				}	
			
		}
		return "保存成功。";
		}
	/**
	 * 保存工序质量控制卡，主副表
	 * @param entity 
	 * @param list
	 * @return
	 */
	public String doSave(Gxzlkzk entity, List<Gxzlkzk_BZ> list) {

		String strbz = "";
		
		if (entity.getId_sno() == 0) {			
			super.save(entity);			
			if(list!=null)
			for (Gxzlkzk_BZ bz : list) {		
				if(bz!=null){
				bz.setP_idsno(entity.getId_sno());
				super.save(bz);
				}
			}			
			
		}else{
				super.update(entity);
				
				if(list!=null){
					super.deleteByDHql(" p_idsno='"+entity.getId_sno()+"' ", null, Gxzlkzk_BZ.class.getName());
					
				for (Gxzlkzk_BZ bz : list) {		
					if(bz!=null){
					bz.setP_idsno(entity.getId_sno());
					bz.setOc_gx(bz.getOc_gx());
					bz.setOc_gybzjyq(bz.getOc_gybzjyq());
					bz.setOc_fxtx(bz.getOc_fxtx());
					bz.setOc_result(bz.getOc_result());
					super.save(bz);
					}
				}		
				}	
			
		}
		return "保存成功。";
		}	
	/**
	 * 保存班组作业控制卡（二种票）
	 * @param entity 主表
	 * @param list 管理人员到位
	 * @param ryzzfgList 人员组织分工
	 * @param wxdfxList 危险点分析
	 * @param file 图片
	 * @return
	 */
	public String doSave(MainWorkCardOne entity, List<WorkCardOne_BZ> list,List<Ryzzfg> ryzzfgList,List<Wxdfx> wxdfxList,TFile file) {

		String strbz = "";
		
		if (entity.getId_sno() == 0) {
			if(file!=null){
			super.save(file);
			entity.setPic_id(file.getId());
			}
			super.save(entity);			
			if(list!=null)
			for (WorkCardOne_BZ bz : list) {	
				if(bz!=null){
				bz.setP_idsno(entity.getId_sno());
				super.save(bz);
				}
			}		
			if(ryzzfgList!=null)
				for (Ryzzfg bz : ryzzfgList) {	
					if(bz!=null){
					bz.setP_idsno(entity.getId_sno());
					super.save(bz);
					}
				}		
			if(wxdfxList!=null)
				for (Wxdfx bz : wxdfxList) {	
					if(bz!=null){
					bz.setP_idsno(entity.getId_sno());
					super.save(bz);
					}
				}				
			
		}else{
			if(file!=null){
				
				super.deleteAll(String.valueOf(entity.getPic_id()), TFile.class.getName());
				super.save(file);				
				entity.setPic_id(file.getId());
				}
				super.update(entity);				
				if(list!=null){
					super.deleteByDHql(" p_idsno='"+entity.getId_sno()+"' ", null, WorkCardOne_BZ.class.getName());
					
				for (WorkCardOne_BZ bz : list) {		
					if(bz!=null){
					bz.setP_idsno(entity.getId_sno());
					bz.setQuetionandsuggest(bz.getQuetionandsuggest().trim());
					super.save(bz);
					}
				}	
				}
				if(ryzzfgList!=null){
					super.deleteByDHql(" p_idsno='"+entity.getId_sno()+"' ", null, Ryzzfg.class.getName());
					
				for (Ryzzfg bz : ryzzfgList) {	
					if(bz!=null){
					bz.setP_idsno(entity.getId_sno());
					bz.setOc_zynr(bz.getOc_zynr().trim());
					super.save(bz);
					}
				}
				}			
				if(wxdfxList!=null){
					super.deleteByDHql(" p_idsno='"+entity.getId_sno()+"' ", null, Wxdfx.class.getName());
					
				for (Wxdfx bz : wxdfxList) {	
					if(bz!=null){
					bz.setP_idsno(entity.getId_sno());
					bz.setOc_wxd(bz.getOc_wxd().trim());
					bz.setOc_ffkzcs(bz.getOc_ffkzcs().trim());
					super.save(bz);
					}
				}					
				
				}	
			
		
		
		}
		return "保存成功。";
		}

	
	
}
